package com.xspace.android.xafcommon.network.apn;

import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import com.xspace.android.xafcommon.appliction.BaseApplication;

public class ApnUtil {
    public static Context context = BaseApplication.mInstance;

    private static Uri PREFERRED_APN_URI = Uri
            .parse("content://telephony/carriers/preferapn");

    private static ApnNode apnNode = new ApnNode();

    private static boolean netWorkChange = true;

    public static String getApnType() {
        return ApnUtil.getDefaultAPN().getApn();
    }

    public static ApnNode getDefaultAPN() {

        if (netWorkChange) {
            String apn = "";
            String proxy = "";
            String name = "";
            String port = "";
            String mcc = "";
            String mnc = "";
            String numeric = "";

            Cursor mCursor = context.getContentResolver().query(
                    PREFERRED_APN_URI, null, null, null, null);
            if (mCursor == null) {
                return null;
            }
            while (mCursor != null && mCursor.moveToNext()) {
                name = mCursor.getString(mCursor.getColumnIndex("name"));
                apn = mCursor.getString(mCursor.getColumnIndex("apn"))
                        .toLowerCase();
                proxy = mCursor.getString(mCursor.getColumnIndex("proxy"));
                port = mCursor.getString(mCursor.getColumnIndex("port"));
                mcc = mCursor.getString(mCursor.getColumnIndex("mcc"));
                mnc = mCursor.getString(mCursor.getColumnIndex("mnc"));
                numeric = mCursor.getString(mCursor.getColumnIndex("numeric"));
            }
            apnNode.setName(name);
            apnNode.setApn(apn);
            apnNode.setProxy(proxy);
            apnNode.setPort(port);
            apnNode.setMcc(mcc);
            apnNode.setMnc(mnc);
            apnNode.setNumeric(numeric);
            System.out.println(apn);
            System.out.println(proxy);
            System.out.println(port);
            System.out.println(mcc);
            System.out.println(mnc);
            System.out.println(numeric);
        }
        return apnNode;
    }

    public static boolean isCmWap() {
        if (isMoblie()) {
            if (getApnType().trim().equals(APNname.CMWAP))
                return true;
            return false;
        }
        return false;
    }

    public static boolean isCmNet() {
        if (isMoblie()) {
            if (getApnType().trim().equals(APNname.CMNET))
                return true;
            return false;
        }
        return false;
    }

    public static boolean isMoblie() {
        try {
            NetworkInfo localNetworkInfo = ((ConnectivityManager) ((Context) ApnUtil.context)
                    .getSystemService(Context.CONNECTIVITY_SERVICE))
                    .getActiveNetworkInfo();
            if (localNetworkInfo != null) {
                if (localNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                    return true;
                return false;
            } else {
                return false;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }

    public static boolean isUniWap() {
        if (isMoblie()) {
            if (getApnType().trim().equals(APNname.UNIWAP))
                return true;
            return false;
        }
        return false;

    }

    public static boolean isWifi() {
        try {
            NetworkInfo localNetworkInfo = ((ConnectivityManager) ((Context) ApnUtil.context)
                    .getSystemService(Context.CONNECTIVITY_SERVICE))
                    .getActiveNetworkInfo();
            if (localNetworkInfo != null) {
                if (localNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)
                    return true;
                return false;
            } else {
                return false;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }

    public class APNname {
        public static final String CMNET = "cmnet";
        public static final String CMWAP = "cmwap";
        public static final String CTNET = "ctnet";
        public static final String CTWAP = "ctwap";
        public static final String CUNET = "3gnet";
        public static final String UNIWAP = "uniwap";
        public static final String WAP_3G = "3gwap";

        public APNname() {

        }
    }

    public static void startApnListener(Context context) {
        ApnUtil.context = context;

        final TelephonyManager telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

        telephonyManager.listen(new PhoneStateListener() {

            public void onDataConnectionStateChanged(int state) {

                switch (state) {
                    case TelephonyManager.DATA_DISCONNECTED: {
                        ApnUtil.netWorkChange = true;
                        System.out.println("Network disconnect:");
                        break;
                    }
                    case TelephonyManager.DATA_CONNECTING: {
                        ApnUtil.netWorkChange = true;
                        System.out.println("Network connecting:");
                        break;
                    }
                    case TelephonyManager.DATA_CONNECTED: {
                        ApnUtil.netWorkChange = true;
                        System.out.println("Network connected:");
                        break;
                    }
                }
            }
        }, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    }

    public static boolean isWap() {
        WifiManager wm = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        if (wm.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
            return false;
        }
        String proxyHost = android.net.Proxy.getDefaultHost();
        int proxyPort = android.net.Proxy.getDefaultPort();
        if (proxyHost != null) {
            return true;
        } else {
            return false;
        }
    }

    public static String getWapName() {
        ConnectivityManager mag = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = mag.getActiveNetworkInfo();
        return info.getExtraInfo().toLowerCase();
    }
}